<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>08.custom_binding | KnockOut</title>
  <script src="../assets/knockout-3.5.1.js"></script>
</head>
<body>

  <div data-bind="allowBindings: true">
    <!-- 绑定为 true，显示 Replacement 1，不显示 Original 1 -->
    <div data-bind="text: 'Replacement 1'">Original 1</div>
  </div>
  
  <div data-bind="allowBindings: false">
    <!-- 绑定为 false 不显示 Replacement 2，而显示 Original 2 -->
    <div data-bind="text: 'Replacement 2'">Original 2</div>
  </div>

  <script type="text/javascript">
    ko.bindingHandlers.allowBindings = {
      init: function(elem, valueAccessor) {
        // value 是 false 时，使用后代绑定
        var shouldAllowBindings = ko.unwrap(valueAccessor());
        return { controlsDescendantBindings: !shouldAllowBindings };
      }
    };

    var vm = {}

    ko.applyBindings(vm);
  </script>

</body>
</html>